package com.lsl.view;

import cn.hutool.core.util.ArrayUtil;
import com.lsl.domain.Employee;
import com.lsl.domain.Programmer;
import com.lsl.service.NameListService;
import com.lsl.service.TeamService;
import com.lsl.utils.TSUtility;
import com.lsl.utils.TeamException;
import com.sun.xml.internal.bind.v2.model.core.ID;

/**
 * @author lishuailong
 * @create 2020-06-29 11:27
 */
public class TeamView {
    private NameListService nameListService = new NameListService();
    private TeamService service = new TeamService();



    /**
     * 以表格形式列出公司所有成员
     */
    private void listAllEmployees(){
        System.out.println("-------------------------------------开发团队调度软件-------------------------------------");
        Employee[] allEmployees = nameListService.getAllEmployees();
        if (ArrayUtil.isEmpty(allEmployees)){
            System.out.println("当前没有员工信息！");
        }else {
            System.out.println("ID\t\t姓名\t\t年龄\t\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用\t\t设备");
            for (Employee employee : allEmployees) {
                System.out.println(employee);
            }
        }
        System.out.println("-------------------------------------------------------------------------------------------");


    }

    /**
     *显示团队成员列表操作
     */
    private void getTeam(){
        System.out.println("-------------------------------------团队成员列表-------------------------------------");

        Programmer[] team = service.getTeam();
        if (ArrayUtil.isNotEmpty(team)){
            System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
            for (Programmer programmer : team) {
                System.out.println(programmer.getMemberId()+"/"+programmer);
            }

        }else {
            System.out.println("团队没有成员！！");
        }
        System.out.println("-------------------------------------------------------------------------------------------");

    }

    /**
     *实现添加成员操作
     */
    private void addMember(){
        System.out.println("---------------------添加成员---------------------");
        System.out.print("请输入要添加的员工ID：");
        int id = TSUtility.readInt();
        try {
            Employee employee = nameListService.getEmployee(id);
            service.addMember(employee);
        } catch (TeamException e) {
            System.out.println("添加失败，原因：" + e.getMessage());
        }
        // 按回车键继续...
        TSUtility.readReturn();
    }

    /**
     * 实现删除成员操作
     */
    private void deleteMember(){
        System.out.println("---------------------删除成员---------------------");
        System.out.print("请输入要删除员工的TID：");
        int id = TSUtility.readInt();
        System.out.print("确认是否删除(Y/N)：");
        char yn = TSUtility.readConfirmSelection();
        if (yn == 'N')
            return;

        try {
            service.removeMember(id);
            System.out.println("删除成功");
        } catch (TeamException e) {
            System.out.println("删除失败，原因：" + e.getMessage());
        }
        // 按回车键继续...
        TSUtility.readReturn();
    }

    /**
     * 主界面显示及控制方法
     */
    public void enterMainMenu(){
        boolean loopFlag = true;
        char c='0';
        while (loopFlag){

            if (c!='1'){
                listAllEmployees();
            }

            System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出  请选择(1-4): ");

            c = TSUtility.readMenuSelection();

            switch (c){
                case '1':

                    getTeam();
                    break;
                case '2':

                    addMember();
                    break;
                case '3':

                    deleteMember();
                    break;
                case '4':

                    System.out.println("确认是否退出(Y/N)");
                    char c1 = TSUtility.readConfirmSelection();
                    if (c1=='Y'){
                        loopFlag=false;
                    }
                    break;
            }
        }
    }

    public static void main(String[] args) {
        TeamView teamView = new TeamView();
        teamView.enterMainMenu();
    }

}
